//
//  YZESigninViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/3/21.
//  Copyright © 2017年 YZE. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <QMUITips.h>
#import "UITextField+Validation.h"
#import "YZESigninViewController.h"
#import "YZENetwork.h"
#import "YZEModel.h"
#import "AppDelegate.h"
#import "SelfTools.h"

#define REQ_ID_login 100001
#define REQ_ID_userinfo 100002

@interface YZESigninViewController()<NetworkUtilsDelegate>

@end

@implementation YZESigninViewController

-(void)viewDidLoad {
    self.navigationController.navigationBarHidden = YES;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    //获取控件
    self.i_mobi = [self.view viewWithTag:10001];
    self.i_pwd = [self.view viewWithTag:10002];
    self.i_vc = [self.view viewWithTag:10003];
    self.btn_getvc = [self.view viewWithTag:10004];
    self.btn_signin = [self.view viewWithTag:10005];
    self.btn_signup = [self.view viewWithTag:10006];
    self.btn_findpwd = [self.view viewWithTag:10007];
    self.v_gap = [self.view viewWithTag:10011];
    self.v_bottomline = [self.view viewWithTag:10012];
    //输入框各种修饰
    [self.i_mobi setValidationType:VALIDATION_TYPE_MOBILE_PHONE_VALIDATED];
    [self.i_pwd setSecureTextEntry:YES];
    //输入框底部边框
//    UIColor *lineColor = [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1];
//    UIView *account_id_line = [[UIView alloc]initWithFrame:CGRectMake(0,self.i_mobi.height - 1, self.i_mobi.width, 1)];
//    account_id_line.backgroundColor = lineColor;
//    [self.i_mobi addSubview:account_id_line];
//    UIView *pwd_line = [[UIView alloc]initWithFrame:CGRectMake(0,self.i_mobi.height - 1, self.i_pwd.width, 1)];
//    pwd_line.backgroundColor = lineColor;
//    [self.i_pwd addSubview:pwd_line];
    
//    self.v_gap.backgroundColor = lineColor;
//    self.v_bottomline.backgroundColor = lineColor;
//    UIView *captha_line = [[UIView alloc]initWithFrame:CGRectMake(0,self.i_mobi.height - 1, self.i_vc.width, 1)];
//    captha_line.backgroundColor = [UIColor lightGrayColor];
//    [self.i_vc addSubview:captha_line];
    
    //获取验证码按钮点击
    [self.btn_getvc addTarget:self action:@selector(onGetvcClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //登录按钮点击
    [self.btn_signin addTarget:self action:@selector(singinClick:) forControlEvents:UIControlEventTouchUpInside];

    
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}

-(void)singinClick:(UIButton*)sender{
    YZEModel *request=[YZEModel sharedInstace];
        [request requestLoginWithUserName:self.i_mobi.text password:self.i_pwd.text delegate:self requestTag:REQ_ID_login];
}
//网络请求返回
-(void)OnSuccess:(id)responseObject requestTag:(int)tag{
    //登录成功，请求用户信息
    if (tag==REQ_ID_login) {
        [[YZEModel sharedInstace] requestUserInfoWithDelegate:self requestTag:REQ_ID_userinfo];
    }
    else if(tag==REQ_ID_userinfo){
        AppDelegate *app=(AppDelegate*)[UIApplication sharedApplication].delegate;
        [app initTabBarController];
    }
}
- (void)OnFail:(NSString *)errorMsg errorCode:(NSInteger)errorCode requestTag:(int)tag{
    if (errorCode==3) {
        [UIView showToastWindowWithMessage:@"医生账号只能使用医生版客户端App!"];
    }
    else {
        [UIView showToastWindowWithMessage:errorMsg];
    }
}
//获取验证码点击响应
-(void)onGetvcClick:(UIButton *) sender{
    if ([SelfTools checkMobile:self.i_mobi.text]) {
        YZEModel *request=[YZEModel sharedInstace];
        [request requestGetCaptcha:self.i_mobi.text delegate:nil requestTag:1];
        [self receiveCheckNumButton];
    }
    else{
        [QMUITips showError:@"请输入正确的手机格式" inView:self.view hideAfterDelay:2];
    }
    
    
}
//获取验证码倒计时
- (void)receiveCheckNumButton{
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.btn_getvc setTitle:@"获取验证码" forState:UIControlStateNormal];
                self.btn_getvc.userInteractionEnabled = YES;
                
            });
        }else{
            int seconds = timeout;
            NSString *strTime = [NSString stringWithFormat:@"%d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                self.btn_getvc.userInteractionEnabled = NO;
                //设置界面的按钮显示 根据自己需求设置
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [self.btn_getvc setTitle:[NSString stringWithFormat:@"重发(%@s)",strTime] forState:UIControlStateNormal];
                [UIView commitAnimations];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

-(void)didReceiveMemoryWarning{
    
}

- (void)updateViewConstraints{
    //NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>SCREEN_WIDTH:%f", SCREEN_WIDTH);
    [super updateViewConstraints];
}

@end
